package com.jeesite.modules.isys.keys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jeesite.modules.isys.common.entity.Result;
import com.jeesite.modules.isys.keys.entity.Abnormal;
import com.jeesite.modules.isys.keys.service.IAbnormalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XYMTOP
 * @since 2023-11-12
 */
@RestController
@RequestMapping("/mybatisplus/abnormal")
public class AbnormalController {


    @Qualifier("abnormalServiceImpl")
    @Autowired
    private IAbnormalService abnormalService;

    /**
     * 根据异常ID获取异常信息
     * @param abnormal 异常实体，包含id
     * @return 异常实体对象
     */
    @GetMapping("/getAbnormalById")
    public Result getAbnormalById(Abnormal abnormal) {



         abnormal = abnormalService
                .getBaseMapper()
                .selectOne(new LambdaQueryWrapper<Abnormal>()
                        .eq(Abnormal::getAbnormalId, abnormal.getAbnormalId())
                        .eq(Abnormal::getDeleted,"0"));

        if (abnormal != null) {
            return Result.ok(abnormal);
        } else {
            return Result.fail();
        }
    }

    /**
     * 更新异常审核状态
     * @param abnormalId 异常ID
     * @param auditStatus 审核状态
     * @return 操作结果
     */
    @PutMapping("/{abnormalId}/audit")
    public ResponseEntity<String> updateAbnormalAuditStatus(@PathVariable String abnormalId, @RequestParam Integer auditStatus) {
        abnormalService.updateAbnormalAuditStatus(abnormalId, auditStatus);
        return new ResponseEntity<>("Audit status updated successfully", HttpStatus.OK);
    }

    /**
     * 获取待审核的异常列表
     * @return 待审核异常列表
     */
    @GetMapping("/pending")
    public ResponseEntity<List<Abnormal>> getPendingAuditAbnormals() {
        List<Abnormal> pendingAbnormals = abnormalService.getPendingAuditAbnormals();
        return new ResponseEntity<>(pendingAbnormals, HttpStatus.OK);
    }

    /**
     * 获取已审核的异常列表
     * @return 已审核异常列表
     */
    @GetMapping("/reviewed")
    public ResponseEntity<List<Abnormal>> getReviewedAbnormals() {
        List<Abnormal> reviewedAbnormals = abnormalService.getReviewedAbnormals();
        return new ResponseEntity<>(reviewedAbnormals, HttpStatus.OK);
    }
}
